<?php

/**
 * 
 * @author yuxun
 *
 */
class IosExamineController extends AdminBaseController {
	
    public function index(){
		$this->setTitle('ios审核服列表');
	
		$filter = new PubFilter();
		$grid   = new PubGrid();
	
		$filter->addFilter('packet', 'input', '包号');
		$filter->addFilter('v_num', 'input', '版本号');
	
		$params = $filter->get();
		$page = $this->R('page', '1');
		$m = $this->R('m');
		$params['m'] = $m;
		$header = IosExamineService::indexHeader();
		if(PubFunc::isAjax()){
			$result = IosExamineService::indexData($params);
			$this->out = array(
					'count'     => $result['count'],
					'data'      => $result['data'],
					'result'    => true
			);
			$this->outType = 'json';
		}else{
			$order  = 'create_time';
			$url = PubFunc::ajaxUrl("?r=iosExamine/index", $params);
			$grid->addGrid($header, $url, array(
					'name'=>'数据',
					'order'=>array($order, 'desc'),
					'type'=>'iosExamine/index',
					'page'=>$page,
					'fix' => array(
							'col' => 4
					)
			));
	
			$this->out['filter'] = $filter->show();
			$this->out['grid'] = $grid->show();
	
		}
	}
	
	public function add(){
		$this->setTitle('添加');
		$typeList = IosExamineService::typeList();
		$this->out['typeList'] = $typeList;
		
		$catList = IosExamineCatService::getList();
		$this->out['catList'] = $catList;
		
		$name_port = 'http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER["SERVER_PORT"];
		$gameNoticeUrl = $name_port.'/api/?r=notice/getLastNotice&channel_id=';
		$this->out['gameNoticeUrl'] = $gameNoticeUrl;
	}
	
	public function doAdd(){
		$ar = PubRequest::AR();
		$count = IosExamineService::repeat(0, 'packet', $this->R('packet'),'v_num', $this->R('v_num'),$this->R('g'));
		if($count > 0){
			$this->showMessage('该包号的版本号已经存在');
		}else{
			$result = IosExamineService::doAdd($ar);
	
			if($result['state'] !== false){
				$this->showMessage($result['msg'], PubFunc::buildUrl("/oss/?r=iosExamine/index"));
			}else{
				$this->showMessage($result['msg']);
			}
		}	
	}
	
	public function edit(){
		$id = $this->R('id');
		$info = IosExamineService::get($id);
		 
		if(empty($info)) $this->showMessage('找不到该信息');
		 
		$this->setTitle('编辑');
		 
		$this->out['info'] = $info;
		 
		$backUrl = PubFunc::buildBackUrl("/oss/?r=iosExamine/index");  //返回路径
		$this->out['backUrl'] = $backUrl;
		 
		$page = $this->R('page');
		$this->out['page'] = $page;
		
		$catList = IosExamineCatService::getList();
		$this->out['catList'] = $catList;
		
		$catInfo = IosExamineCatService::get($info['category']);
		$this->out['catInfo'] = $catInfo;
	}
	
	public function doEdit(){
		$ar = PubRequest::AR();
		$id = $this->R('id');
		$count = IosExamineService::repeat($id, 'packet', $this->R('packet'),'v_num', $this->R('v_num'),$this->R('g'));
		if($count > 0){
			$this->showMessage('该包号的版本号已经存在');
		}else{
			$result = IosExamineService::doEdit($id, $ar);
			
			if($result !== false){
				$this->showMessage('编辑成功', PubFunc::buildBackUrl("/oss/?r=iosExamine/index"));
			}else{
				$this->showMessage($result['msg']);
			}
		}
	}
	

	public function delete(){
		$id = $this->R('id');
	    $result = IosExamineService::delete($id);
	    if($result['state']  !== false){
	    	$this->showMessage($result['msg'], PubFunc::buildBackUrl("/oss/?r=iosExamine/index"));
	    }else{
	    	$this->showMessage($result['msg'], PubFunc::buildBackUrl("/oss/?r=iosExamine/index"));
	    }	 
	}
	
	public function change_visable(){
		$id = $this->R('id');
		$val = $this->R('val');
	
		if($val == 1){  //修改为审核服
			$out = array('status'=>0,'msg'=>'不能修改为审核服!','data'=>'');
		}else if($val == 2){ //修改为正式服
			$result = IosExamineService::change_field($id,'type',$val);
				
			if(!$result){
				$out = array('status'=>0,'msg'=>'修改失败','data'=>'');
			}else{
				// 记录到文件中
				$out = array('status'=>1,'msg'=>'修改成功','data'=>'');
			}
				
		}else{
			$out = array('status'=>0,'msg'=>'非法参数!','data'=>'');
		}
		$this->out = $out;
		$this->outType = 'json';
	}


}